package com.wish.blessingroad.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Http {
	public static String httpPost(String strURL, String params, String charset) {
		String rStr = null;
		byte[] byteParams = params.getBytes();
		rStr = Http.httpPostForm(strURL, byteParams, charset);
		return rStr;
	}

	public static String httpPostForm(String strURL, byte[] params, String charset) {
		String rStr = null;
		byte[] buf = null;
		URL url = null;
		HttpURLConnection urlConn = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			StringBuffer sb = new StringBuffer();
			url = new URL(strURL);
			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setConnectTimeout(10000);
			urlConn.setReadTimeout(30000);
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("POST");
			urlConn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			bos = new BufferedOutputStream(urlConn.getOutputStream());
			bos.write(params);
			bos.flush();
			bis = new BufferedInputStream(urlConn.getInputStream());
			// System.out.println(bis.available());
			buf = new byte[1024 * 10];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			while ((len = bis.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}
			buf = baos.toByteArray();
			rStr = new String(buf, charset);

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null)
					bos.close();
			} catch (Exception e) {

			}
			try {
				if (bis != null)
					bis.close();
			} catch (Exception e) {

			}
			try {
				if (urlConn != null)
					urlConn.disconnect();
			} catch (Exception e) {

			}
		}
		return rStr;

	}

	public static String httpPostText(String strURL, byte[] params, String charset) {
		String rStr = null;
		byte[] buf = null;
		URL url = null;
		HttpURLConnection urlConn = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {
			StringBuffer sb = new StringBuffer();
			url = new URL(strURL);
			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setConnectTimeout(10000);
			urlConn.setReadTimeout(30000);
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("POST");
			urlConn.setRequestProperty("Content-Type", "text/xml");
			urlConn.setRequestProperty("Content-length",
					String.valueOf(params.length));
			bos = new BufferedOutputStream(urlConn.getOutputStream());
			bos.write(params);
			bos.flush();
			bis = new BufferedInputStream(urlConn.getInputStream());
			// System.out.println(bis.available());
			buf = new byte[1024 * 10];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			while ((len = bis.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}
			buf = baos.toByteArray();
			rStr = new String(buf, charset);

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null)
					bos.close();
			} catch (Exception e) {

			}
			try {
				if (bis != null)
					bis.close();
			} catch (Exception e) {

			}
			try {
				if (urlConn != null)
					urlConn.disconnect();
			} catch (Exception e) {

			}
		}
		return rStr;

	}

	public static String httpGet(String strURL, String charset)
			throws Exception {
		String rStr = null;
		byte[] buf = null;
		URL url = null;
		HttpURLConnection urlConn = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {
			StringBuffer sb = new StringBuffer();
			url = new URL(strURL);
			// System.out.println(url);
			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setConnectTimeout(10000);
			urlConn.setReadTimeout(30000);
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("GET");
			urlConn.setRequestProperty("Accept", "*/*");
			urlConn.setRequestProperty("Content-Type", "text/html");
			bis = new BufferedInputStream(urlConn.getInputStream());
			// System.out.println(bis.available());
			buf = new byte[1024 * 100];
			int i = 0;
			int total = 0;
			while ((i = bis.read(buf, total, buf.length - total)) != -1) {
				total += i;
			}
			rStr = new String(buf, 0, total, charset);

		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (bos != null)
					bos.close();
			} catch (Exception e) {

			}
			try {
				if (bis != null)
					bis.close();
			} catch (Exception e) {

			}
			try {
				if (urlConn != null)
					urlConn.disconnect();
			} catch (Exception e) {

			}
		}
		return rStr;

	}

	public static void main(String[] args) {
		String url = "http://223.203.192.235:8080/qxt/receivercfg";
		System.out.println(Http.httpPostText(url, "test".getBytes(), "gbk"));
	}
}
